/*******************************************************************************
 * Copyright (C) 2010 Devin Samarin.
 * 
 * This file is part of the SUIT Toolkit.
 * 
 * The SUIT Toolkit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The SUIT Toolkit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SUIT Toolkit.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
import suit.ui.Application;
import suit.ui.Box;
import suit.ui.Button;
import suit.ui.Entry;
import suit.ui.Screen;
import suit.ui.themes.DefaultTheme;

public class Calculator extends Application {
	private Entry entry;
	private Screen main;

	private double total = 0;
	private double operand = 0;
	private int operator = 0;

	private boolean newnum = true;

	public static final int ADD = 1;
	public static final int SUB = 2;
	public static final int MUL = 3;
	public static final int DIV = 4;

	public Calculator() {
		main = new Screen(new DefaultTheme());
		entry = new Entry("0");
		Button add = new Button("+");
		add.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				operator = ADD;
				operand = Double.parseDouble(entry.getText());
				newnum = true;
			}
		});
		Button sub = new Button("-");
		sub.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				operator = SUB;
				operand = Double.parseDouble(entry.getText());
				newnum = true;
			}
		});
		Button mul = new Button("*");
		mul.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				operator = MUL;
				operand = Double.parseDouble(entry.getText());
				newnum = true;
			}
		});
		Button div = new Button("/");
		div.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				operator = DIV;
				operand = Double.parseDouble(entry.getText());
				newnum = true;
			}
		});
		Button eq = new Button("=");
		eq.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				calc();
			}
		});

		Button z = new Button("0");
		z.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "0");
				newnum = false;
			}
		});
		Button i = new Button("1");
		i.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "1");
				newnum = false;
			}
		});
		Button ii = new Button("2");
		ii.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "2");
				newnum = false;
			}
		});
		Button iii = new Button("3");
		iii.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "3");
				newnum = false;
			}
		});
		Button iv = new Button("4");
		iv.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "4");
				newnum = false;
			}
		});
		Button v = new Button("5");
		v.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "5");
				newnum = false;
			}
		});
		Button vi = new Button("6");
		vi.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "6");
				newnum = false;
			}
		});
		Button vii = new Button("7");
		vii.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "7");
				newnum = false;
			}
		});
		Button viii = new Button("8");
		viii.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "8");
				newnum = false;
			}
		});
		Button ix = new Button("9");
		ix.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "") + "9");
				newnum = false;
			}
		});

		Button p = new Button(".");
		p.connect(new Button.Clicked() {
			public void onClicked(Button source) {
				entry.setText(((!newnum) ? entry.getText() : "0") + ".");
				newnum = false;
			}
		});

		Box vbox = new Box(Box.Vert, true, 10);
		vbox.setBorderWidth(10);

		Box hbox1 = new Box(Box.Horiz, true, 10);
		hbox1.pack(add, true, true, 0);
		hbox1.pack(sub, true, true, 0);
		hbox1.pack(mul, true, true, 0);
		hbox1.pack(div, true, true, 0);

		Box hbox2 = new Box(Box.Horiz, true, 10);
		hbox2.pack(vii, true, true, 0);
		hbox2.pack(viii, true, true, 0);
		hbox2.pack(ix, true, true, 0);
		hbox2.pack(eq, true, true, 0);

		Box hbox3 = new Box(Box.Horiz, true, 10);
		hbox3.pack(iv, true, true, 0);
		hbox3.pack(v, true, true, 0);
		hbox3.pack(vi, true, true, 0);
		hbox3.pack(p, true, true, 0);

		Box hbox4 = new Box(Box.Horiz, true, 10);
		hbox4.pack(i, true, true, 0);
		hbox4.pack(ii, true, true, 0);
		hbox4.pack(iii, true, true, 0);
		hbox4.pack(z, true, true, 0);

		vbox.pack(entry, true, true, 0);
		vbox.pack(hbox1, true, true, 0);
		vbox.pack(hbox2, true, true, 0);
		vbox.pack(hbox3, true, true, 0);
		vbox.pack(hbox4, true, true, 0);

		main.add(vbox);
		setScreen(main);

	}

	public void calc() {
		newnum = true;
		double e = Double.NaN;
		try {
			e = Double.parseDouble(entry.getText());
			if (operator != 0) {
				if (operator == ADD) {
					total = operand + e;
				} else if (operator == SUB) {
					total = operand - e;
				} else if (operator == MUL) {
					total = operand * e;
				} else if (operator == DIV) {
					total = operand / e;
				}
			}
		} catch (Exception s) {
			total = Double.NaN;
		}
		entry.setText(Double.toString(total));
	}

	public boolean onKeyPressed(int key) {
		if (key == -8) {
			if (!newnum) {
				String e = entry.getText();
				e = e.substring(0, e.length() - 1);
				if (e.length() == 0) {
					e = "0";
					total = 0;
				}
				entry.setText(e);
			} else {
				if (entry.getText() == "0")
					notifyDestroyed();
				entry.setText("0");
			}
		} else if (key == -103) {
			notifyDestroyed();
		}
		return false;
	}
}
